package com.touzhijia.scheduler.controller;

import com.touzhijia.common.controller.BaseController;
import com.touzhijia.common.utils.BeanValidatorUtils;
import com.touzhijia.common.vo.response.AjaxMessage;
import com.touzhijia.scheduler.service.HttpServiceJobService;
import com.touzhijia.scheduler.service.QuartzTriggerService;
import com.touzhijia.scheduler.vo.model.HttpJobNotice;
import com.touzhijia.scheduler.vo.model.HttpJobResult;
import com.touzhijia.scheduler.vo.request.AddHttpServiceJobVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Http任务 Controller
 * Created by lzw on 2017/6/4.
 */
@Controller
@RequestMapping(value = "/quartz/httpJob")
public class HttpServiceJobController extends BaseController {

    @Autowired
    private HttpServiceJobService httpServiceJobService;

    @Autowired
    private QuartzTriggerService triggerService;

    @RequestMapping("/addHttpServiceJob" + REQUEST_SUFFIX)
    @ResponseBody
    public AjaxMessage<String> addHttpServiceJob(@Valid @RequestBody AddHttpServiceJobVo addHttpServiceJobVo, BindingResult bindingResult) {
        AjaxMessage<String> ajaxMessage = new AjaxMessage<>(true, "新增Job成功", null);
        if (BeanValidatorUtils.beanValidator(bindingResult, ajaxMessage)) {
            if (triggerService.validatorCron(addHttpServiceJobVo.getCron(), 1, ajaxMessage) == null) {
                return ajaxMessage;
            }
            httpServiceJobService.addHttpServiceJob(addHttpServiceJobVo, ajaxMessage);
        }
        return ajaxMessage;
    }


    // ==============================================================================================================
    @RequestMapping("/test01" + REQUEST_SUFFIX)
    @ResponseBody
    public Object test01() {
        HttpJobResult httpJobResult = new HttpJobResult();
        httpJobResult.setSuccess(true);
        httpJobResult.setMessage("任务执行成功");
        httpJobResult.setStartJobTime(new Date());
        httpJobResult.setEndJobTime(new Date());
        httpJobResult.setExceptionStack("Error Stack ....");
        Map<String, String> resultData = new HashMap<>();
        resultData.put("key01", "value01");
        resultData.put("key02", "value02");
        resultData.put("key03", "value03");
        httpJobResult.setResultData(resultData);

        HttpJobNotice notice = new HttpJobNotice();
        notice.setNoticeType("短信");
        notice.setSendTo("13243895946");
        notice.setMessageTemplate("任务执行失败, 任务名:${jobName}, 失败原因${message}"); // 可以使用请求参数的变量
        Map<String, String> messageData = new HashMap<>();
        messageData.put("jobName", "测试任务");
        messageData.put("message", "网络异常");
        notice.setMessageData(messageData);
        httpJobResult.setNotice(notice);
        return httpJobResult;
    }

}
